<!--  -->
<template>
  <el-container class="home-container">
    <el-header>
      <div class="logo-box">
        <img src="../assets/logo.png" />
      </div>
      <span>广财小站-校园闲置平台</span>
    </el-header>

    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="togglrCollapse">|||</div>
        <el-menu background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <template v-for="item in menulist">
            <el-submenu v-if="item.children.length>0" :index="item.id + ''"  :key="item.id">
              <template slot="title">
                <i :class="icons[item.id]"></i>
                <span>{{ item.menuname }}</span>
              </template>
              <el-menu-item-group>
                <el-menu-item  :index="subItem.path + ''" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState(subItem.path)">
                  <template slot="title">
                    <i :class="icons[subItem.id]"></i>
                    <span>{{ subItem.menuname }}</span>
                  </template>
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
            <el-menu-item v-else :index="item.path + ''"  :key="item.id" @click="saveNavState(item.path)">
                <i :class="icons[item.id]"></i>
                <span slot="title">{{ item.menuname }}</span>
            </el-menu-item>
          </template>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';

export default {
  // import引入的组件需要注入到对象中才能使用
  components: {},
  data () {
    // 这里存放数据
    return {
      menulist: [],
      icons: {
        1: 'el-icon-pie-chart',
        2: 'el-icon-s-goods',
        5: 'el-icon-success',
        6: 'el-icon-menu',
        3: 'el-icon-tickets',
        4: 'el-icon-s-shop',
        7: 'el-icon-user',
        8: 'el-icon-tickets',
        9: 'el-icon-present',
        10: 'el-icon-s-claim'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    async getMenuList () {
      const { data: res } = await this.$axios.get('admin/getmenu')
      if (res.code !== 200) {
        return this.$message.error(res.msg)
      }
      this.menulist = res.data
    },
    togglrCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {},
  beforeCreate () {
    this.activePath = window.sessionStorage.getItem('activePath')
  }, // 生命周期 - 创建之前
  beforeMount () {}, // 生命周期 - 挂载之前
  beforeUpdate () {}, // 生命周期 - 更新之前
  updated () {}, // 生命周期 - 更新之后
  beforeDestroy () {}, // 生命周期 - 销毁之前
  destroyed () {}, // 生命周期 - 销毁完成
  activated () {} // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang='scss' scoped>
.home-container {
  height: 100%;
}
.el-header {
  background: #333333;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
  font-size: 20px;

  span {
    font-weight: 600;
    margin-left: 60px;
  }

  // padding: 8px 6px;
}
.logo-box {
  width: 48px;
  height: 48px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 4px;
  box-shadow: 0 0 5px #eee;
  background-color: #fff;
  position: absolute;
}
.logo-box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
.el-aside {
  background: #545c64;
}
.el-main {
  background: #f4f4f4;
}
.el-menu {
  border-right-width: 0;
}
.toggle-button {
  background-color: #3b4754;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
